var indexTemplate = {};


indexTemplate.indexList = [

    '<div class="well">',
    '<div class="span2 pull-left"><a href="#refreshindices" class="btn btn-mini"  rel="tipRight" data-placement="bottom" data-html="true" data-title="Refreshing every <%- polling/1000 %> seconds.<br/>Click to Force Refresh."><i class="icon-refresh"></i> <%- lastUpdateTime %></a></div>',
    '<div class="text-center span8"><span style="font-size: 28px;">Indices Overview</span></div>',
/*
    '<div class="pull-right">',
    '<a href="#visualize" class="btn btn-info" rel="tipRight" data-placement="bottom" data-html="true" data-title="View Indices Distribution across Nodes">',
    '<i class="icon-sitemap"></i> Indices Distribution</a>',
    '</div>',
*/
    '</div>',

    '<div class="span11 center-table">',

    '<div id="toolbar" class="pull-right" style="padding-bottom: 10px;">',
    '<div class="btn-group">',
    '<a href="#refreshall" class="btn" rel="popRight" data-trigger="hover" data-placement="bottom" data-title="Refresh all Indices" ',
    'data-content="Makes all operations performed since the last refresh available for search. The (near) real-time capabilities depend on the index engine used. For example, the robin one requires refresh to be called, but by default a refresh is scheduled periodically."',
    '><i class="icon-refresh"></i> Refresh</a>',
    '<a href="#optimizeall" class="btn" rel="popRight" data-trigger="hover" data-placement="bottom" data-title="Optimize all Indices" ',
    'data-content="The optimize process basically optimizes the index for faster search operations (and relates to the number of segments a lucene index holds within each shard). The optimize operation allows to reduce the number of segments by merging them."',
    '><i class="icon-rocket"></i> Optimize</a>',
    '<a href="#flushall" class="btn" rel="popRight" data-trigger="hover" data-placement="bottom" data-title="Flush all Indices" ',
    'data-content="The flush process of an index basically frees memory from the index by flushing data to the index storage and clearing the internal transaction log. By default, ElasticSearch uses memory heuristics in order to automatically trigger flush operations as required in order to clear memory.">',
    '<i class="icon-rotate-right"></i> Flush</a>',
    '<a href="#clearcacheall" class="btn" rel="popRight" data-trigger="hover" data-placement="bottom" data-title="Clear all Caches" data-content="Clears the cache on all indices."><i class="icon-eraser"></i> Clear Cache</a>',
    '</div> <!-- btn group -->',
    '</div> <!-- toolbar --> ',
    '<div id="indicesToolbar" class="pull-left" style="padding-bottom: 10px;">',
    //'<div class="btn-group">',
    '<a href="#createindex" class="btn" rel="popRight" data-trigger="hover" data-placement="bottom" data-title="Create Index" data-content="Create a New Index on your cluster."><i class="icon-edit"></i> Create Index</a>',
    //'</div> <!-- btn group -->',
    '</div> <!-- toolbar --> ',

    '<table class="table table-bordered table-striped table-hover" id="indicesTable">',
    '<thead>',
    '<tr><th>Index</th><th># Docs</th><th>Primary Size</th><th># Shards</th><th># Replicas</th><th>Status</th></tr>',
    '</thead>',
    '<tbody>',
    '<% _.each(indices, function(index) { %>',
    '<tr><td>',
    '<a href="#index/<%- index.id %>"  rel="tipRight" data-placement="bottom" data-title="Index Information"><%- index.name %></a>',
    '</td><td><%- numeral(index.docs.num_docs).format("0,0") %></td><td><%- numeral(index.index.primary_size_in_bytes).format("0.0b")  %></td><td><%- numeral(index.numshards).format("0,0") %></td><td><%- numeral(index.numreplicas).format("0,0") %></td><td><%- index.status %></td></tr>',
    '<% }); %>',
    '</tbody>',
    '</table>',
    '</div>'

].join("\n");

indexTemplate.indexView = [
    '<div class="modal hide fade" id="deleteindexmodal">',
    '<div class="modal-header">',
    '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>',
    '<h3>WARNING!</h3>',
    '</div>',
    '<div class="modal-body">',
    '<p>Are you sure you want to delete this index?<br/><br/>This action will delete the index and its data!</p>',
    '</div>',
    '<div class="modal-footer">',
    '<a href="#" class="btn" data-dismiss="modal">Close</a>',
    '<a href="#deleteindex/<%- indexId %>" class="btn btn-danger">Delete</a>',
    '</div>',
    '</div>',

    '<div class="modal hide fade" id="deletealiasmodal">',
    '<div class="modal-header">',
    '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>',
    '<h3>WARNING!</h3>',
    '</div>',
    '<div class="modal-body">',
    '<p>Are you sure you want to delete this alias?<br/><br/>This action cannot be undone!</p>',
    '<input type="hidden" name="deleteAliasId" id="deleteAliasId" value="">',
    '</div>',
    '<div class="modal-footer">',
    '<a href="#" class="btn" data-dismiss="modal">Close</a>',
    '<button class="btn btn-danger" id="deleteIndexBtn">Delete</a>',
    '</div>',
    '</div>',

    '<div class="pull-left"><a href="#refreshindexpoller/<%- indexId %>" class="btn btn-mini"  rel="tipRight" data-placement="bottom" data-html="true" data-title="Refreshing every <%- polling/1000 %> seconds.<br/>Click to Force Refresh."><i class="icon-refresh"></i> <%- lastUpdateTime %></a></div>',

    '<div class="text-center"><h2><%- indexName %></h2>',
    '<div class="span12 center-table">',
    '<ul class="nav nav-tabs">',
    '<li><a href="#metrics"  class="active" data-toggle="tab" id="indexTab">Metrics</a></li>',
    '<li><a href="#shards" data-toggle="tab" id="shardTab">Shards</a></li>',
    '<li><a href="#aliases" data-toggle="tab" id="aliasTab">Aliases</a></li>',
    '<li><a href="#administration" data-toggle="tab" id="adminTab">Administration</a></li>',
    '</ul>',
    '<div class="tab-content">',
    '<div class="tab-pane active" id="metrics">',

    '<div class="row center-table">',
    '<% if (isOpenState == true) { %>',
    '<div class="span3">',
    '<div class="well"><span class="stat-detail"><%- numeral(index.docs.num_docs).format("0,0") %></span><span>Documents</span>',
    '</div></div>',
    '<div class="span3">',
    '<div class="well"><span class="stat-detail"><%- numeral(index.index.primary_size_in_bytes).format("0.0b") %></span><span>Primary Size</span>',
    '</div></div>',
    '<div class="span3">',
    '<div class="well"><span class="stat-detail"><%- numeral(index.index.size_in_bytes).format("0.0b") %></span><span>Total Size</span>',
    '</div></div>',
    '<div class="span3">',
    '<div class="well"><span class="stat-detail"><%- numeral(totalShards.total).format("0,0") %></span><span>Total Shards</span>',
    '</div></div>',

    '</div>',

    '<div class="row-fluid">',
    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-file-alt"></i></div>',
    '<span class="text-left">Health</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',
    '<tr><td class="span3">Status:</td><td><span class="label label-<%- index.statusClassLabel %>"><%- index.statusText %></span></td></tr>',
    '<tr><td>Nodes:</td><td><%- numeral(index.number_of_nodes).format("0,0") %></td></tr>',
    '<tr><td>Data Nodes:</td><td><%- numeral(index.number_of_data_nodes).format("0,0") %></td></tr>',
    '<tr><td>Primary Shards:</td><td><%- numeral(index.active_primary_shards).format("0,0") %></td></tr>',
    '<tr><td>Active Shards:</td><td><%- numeral(index.active_shards).format("0,0") %></td></tr>',
    '<tr><td>Relocating Shards:</td><td><%- numeral(index.relocating_shards).format("0,0") %></td></tr>',
    '<tr><td>Initializing Shards:</td><td><%- numeral(index.initializing_shards).format("0,0") %></td></tr>',
    '<tr><td>Unassigned Shards:</td><td><%- numeral(index.unassigned_shards).format("0,0") %></td></tr>',
    '</table>',

    '</div>', // span6

    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-file-alt"></i></div>',
    '<span class="text-left">Documents</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',
    '<tr><td class="span3">Documents:</td><td><%- numeral(index.docs.num_docs).format("0,0") %></td></tr>',
    '<tr><td>Max Documents:</td><td><%- numeral(index.docs.max_doc).format("0,0") %></td></tr>',
    '<tr><td>Deleted Documents:</td><td><%- numeral(index.docs.deleted_docs).format("0,0") %></td></tr>',
    '<tr><td>Primary Size:</td><td><%- numeral(index.index.primary_size_in_bytes).format("0.0b")  %></td></tr>',
    '<tr><td>Total Size:</td><td><%- numeral(index.index.size_in_bytes).format("0.0b")  %></td></tr>',
    '</table>',

    '</div>', // span6
    '</div> <!-- end row -->',

    '<div class="row center-table">',
    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-search"></i></div>',
    '<span class="text-left">Search Totals</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',

    '<tbody>',
    '<tr><td class="span3">Query Total:</td><td><%- numeral(index.total.search.query_total).format("0,0") %></td></tr>',
    '<tr><td>Query Time:</td><td><%- timeUtil.convertMS(index.total.search.query_time_in_millis) %></td></tr>',
    '<tr><td>Fetch Total:</td><td><%- numeral(index.total.search.fetch_total).format("0,0") %></td></tr>',
    '<tr><td>Fetch Time:</td><td><%- timeUtil.convertMS(index.total.search.fetch_time_in_millis) %></td></tr>',
    '</tbody>',
    '</table>',
    '</div>', // span6


    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-list"></i></div>',
    '<span class="text-left">Indexing Totals</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',

    '<tr><td class="span3">Index Total:</td><td><%- numeral(index.total.indexing.index_total).format("0,0") %></td></tr>',
    '<tr><td class="span3">Index Time:</td><td><%- timeUtil.convertMS(index.total.indexing.index_time_in_millis) %></td></tr>',
    '<tr><td>Delete Total:</td><td><%- numeral(index.total.indexing.delete_total).format("0,0") %></td></tr>',
    '<tr><td>Delete Time:</td><td><%- timeUtil.convertMS(index.total.indexing.delete_time_in_millis) %></td></tr>',
    '</table>',
    '</div>', // span6
    '</div> <!-- end row -->',


    '<div class="row center-table">',
    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-upload-alt"></i></div>',
    '<span class="text-left">Get Totals</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',

    '<tr><td class="span3">Get Total:</td><td><%- numeral(index.total.get.total).format("0,0") %></td></tr>',
    '<tr><td>Get Time:</td><td><%- timeUtil.convertMS(index.total.get.time_in_millis) %></td></tr>',
    '<tr><td>Exists Total:</td><td><%- numeral(index.total.get.exists_total).format("0,0") %></td></tr>',
    '<tr><td>Exists Time:</td><td><%- timeUtil.convertMS(index.total.get.exists_time_in_millis) %></td></tr>',
    '<tr><td>Missing Total:</td><td><%- numeral(index.total.get.missing_total).format("0,0") %></td></tr>',
    '<tr><td>Missing Time:</td><td><%- timeUtil.convertMS(index.total.get.missing_time_in_millis) %></td></tr>',
    '</table>',
    '</div>',

    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-cogs"></i></div>',
    '<span class="text-left">Operations</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',

    //'<tr><td class="span3">Refresh Total:</td><td><%- numeral(index.refresh.total).format("0,0") %></td></tr>',
    //'<tr><td class="span3">Refresh Time:</td><td><%- timeUtil.convertMS(index.refresh.total_time_in_millis) %></td></tr>',
    //'<tr><td>Flush Total:</td><td><%- numeral(index.flush.total).format("0,0") %></td></tr>',
    //'<tr><td>Flush Time:</td><td><%- timeUtil.convertMS(index.flush.total_time_in_millis) %></td></tr>',
    '</table>',
    '</div>',

    '<div class="span6">',
    '<table class="table table-bordered table-striped grid-table">',
    '<thead>',
    '<tr><td colspan="2"><div class="grid-table-title">',
    '<div class="pull-left">',
    '<div class="icon-title"><i class="icon-copy"></i></div>',
    '<span class="text-left">Merge Activity</span>',
    '</div>',
    '</div>',
    '</td></tr>',
    '</thead>',

    //'<tr><td class="span3">Merge Total:</td><td><%- numeral(index.merges.total).format("0,0") %></td></tr>',
    //'<tr><td>Merge Total Time:</td><td><%- timeUtil.convertMS(index.merges.total_time_in_millis) %></td></tr>',
    //'<tr><td>Merge Total Docs:</td><td><%- numeral(index.merges.total_docs).format("0,0") %></td></tr>',
    //'<tr><td>Merge Total Size:</td><td><%- numeral(index.merges.total_size_in_bytes).format("0.0b") %></td></tr>',
    '</table>',
    '</div>',

    '<% } else { %>',
    '<div class="lead">You must open the index to see any statistics.</div> ',
    '<% } %>',
    '</div> <!-- end row -->',

    '</div> <!-- end tab -->',

    '<div class="tab-pane" id="shards">',
    '<% if (isOpenState == true) { %>',

    '<div class="row center-table">',
    '<div class="span12">',
    '<table class="table table-bordered table-striped" id="shardTable">',
    '<thead>',
    '<tr><th>Shard</th><th>State</th><th># Docs</th><th>Size</th><th>Primary?</th><th>Node</th></tr>',
    '</thead>',
    '<tbody>',
    '<% _.each(shards, function(shard) { %>',
    '<tr><td><%- shard.routing.shard %></td><td><%- shard.state %></td><td><%- numeral(shard.docs.num_docs).format("0,0") %></td><td><%- numeral(shard.index.size_in_bytes).format("0.0b") %></td><td><%- shard.routing.primary %></td><td><%- shard.node %></td></tr>',
    '<% }) %>',
    '</tbody>',
    '</table>',
    '</div>',
    '</div>',

    '<% } else { %>',
    '<div class="lead">You must open the index to see any statistics.</div> ',
    '<% } %>',
    '</div>',

    '<div class="tab-pane" id="aliases">',
    '<% if (isOpenState == true) { %>',

    '<div class="row center-table">',
    '<div class="span12">',
    '<div id="aliasToolbar" class="pull-left" style="padding-bottom: 10px;">',
    '<a href="#createalias/<%- indexId %>" class="btn" rel="tipRight" data-trigger="hover" data-placement="bottom" data-title="Create a New Alias on this Index."><i class="icon-edit"></i> Create Alias</a>',
    '</div> <!-- toolbar --> ',
    '<% if (!jQuery.isEmptyObject(index.aliases)) { %>',
    '<table class="table table-bordered table-striped" id="aliasesTable">',
    '<thead>',
    '<tr><th>Alias</th><th>Index Routing</th><th>Search Routing</th><th>Filter</th><th>Action</th></tr>',
    '</thead>',
    '<tbody>',
    '<% _.each(index.aliases, function(item, key, list) { %>',
    '<tr><td><%- key %></td><td><%- item.index_routing %></td><td><%- item.search_routing %></td><td>',
    '<% if (item.filter) { %>',
    '<pre class="prettyprint linenums language-json"><%- JSON.stringify(item.filter) %></pre>',
    '<% } %>',
    '</td>',
    '<td><a href="#deletealiasmodal" data-id="<%- key %>" role="button" class="btn btn-block btn-danger opendeletealiasmodal" style="white-space: nowrap">Delete</a></td>',
    '</tr>',
    '<% }) %>',
    '</tbody>',
    '</table>',
    '<% } else { %>',
    '<div class="lead">No aliases associated with this index.</div> ',
    '<% } %>',
    '</div>',
    '</div>',

    '<% } else { %>',
    '<div class="lead">You must open the index to see any alias information.</div> ',
    '<% } %>',
    '</div>',

    '<div class="tab-pane" id="administration">',

    '<table class="table table-bordered table-striped">',
    '<% if (isOpenState == true) { %>',
    '<tr><td><a href="#flushindex/<%- indexId %>" class="btn btn-block" style="white-space: nowrap;">Flush Index</a></td><td>The flush process of an index frees memory from the index by flushing data to the index storage and clearing the internal transaction log. By default, ElasticSearch uses memory heuristics in order to automatically trigger flush operations as required in order to clear memory.</td></tr>',
    '<tr><td><a href="#clearcacheindex/<%- indexId %>" class="btn btn-block" style="white-space: nowrap;">Clear Cache</a></td><td>Clears the cache on all indices.</td></tr>',
    '<tr><td><a href="#optimizeindex/<%- indexId %>" class="btn btn-block" style="white-space: nowrap;">Optimize Index</a></td><td>The optimize process basically optimizes the index for faster search operations (and relates to the number of segments a lucene index holds within each shard). The optimize operation allows to reduce the number of segments by merging them.</td></tr>',
    '<tr><td><a href="#refreshindex/<%- indexId %>" class="btn btn-block" style="white-space: nowrap;">Refresh Index</a></td><td>Refresh the index, making all operations performed since the last refresh available for search. The (near) real-time capabilities depend on the index engine used. For example, the robin one requires refresh to be called, but by default a refresh is scheduled periodically.</td></tr>',
    '<tr><td><a href="#closeindex/<%- indexId %>" class="btn btn-warning btn-block" style="white-space: nowrap;">Close Index</a></td>',
    '<td>The open and close index commands allow to close an index, and later on opening it. A closed index has almost no overhead on the cluster (except for maintaining its metadata), and is blocked for read/write operations. A closed index can be opened which will then go through the normal recovery process.</td></tr>',
    '<tr><td><a href="#deleteindexmodal" data-toggle="modal" role="button" class="btn btn-block btn-danger" rel="popRight" data-trigger="hover" data-placement="bottom" style="white-space: nowrap">Delete Index</a></td><td><strong>WARNING! This action cannot be undone. You will destroy this index and all documents associated with this, by clicking this button.</strong></td></tr>',
    '<% } else { %>',
    '<tr><td><a href="#openindex/<%- indexId %>" class="btn btn-block" style="white-space: nowrap;">Open Index</a></td>',
    '<td>The open and close index commands allow to close an index, and later on opening it. A closed index has almost no overhead on the cluster (except for maintaining its metadata), and is blocked for read/write operations. A closed index can be opened which will then go through the normal recovery process.</td></tr>',
    '<% } %>',
    '</table>',
    '</div>',

    '</div>',
    '</div></div>'
].join("\n");